import IPrefable from "../reflect/IPrefable";
import Component from "../Component";
import PopupWindow from "../PopupWindow";
import Dialog from "./Dialog";

export abstract class WindowContent
    extends Component implements IPrefable {

    get window(): PopupWindow {
        return this._window;
    }

    set window(wind: PopupWindow) {
        this._window = wind;
    }

    public readonly path: string;
    public readonly winName: string;
    private _window: any = null;

    public show() {
        this._window.show();
    }

    public async dismiss() {
        if (!this._window) {
            return;
        }
        return this.onDismiss().then(() => {
            return this._window.finish();
        });
    }

    public onShowing() {
        // const action = cc.spawn(cc.moveBy(0.4, 0, -1000).easing(cc.easeElasticOut(2)), cc.fadeIn(0.4));
        // this.node.y += 1000;
        this.node.scale = 0.2;
        this.node.opacity = 0;
        const action = cc.spawn(cc.fadeIn(0.1), cc.scaleTo(0.1, 1, 1).easing(cc.easeIn(2)));
        this.node.runAction(action);
    }

    public async onDismiss() {
        return new Promise(resolve => {
            const action = cc.spawn(cc.fadeOut(0.1), cc.scaleTo(0.1, 0.2, 0.2).easing(cc.easeOut(2)));
            this.node.runAction(cc.sequence(action, cc.callFunc(() => {
                resolve();
            })));
        })
    }
}

export class DialogContent extends WindowContent {
    get dialog(): Dialog {
        return this.window as Dialog;
    }
}
